package com.sita.bike.ui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

/***
 * 自定义进度条
 *
 * @author spring sky
 *         Email:vipa1888@163.com
 *         创建时间：2014-1-6下午3:28:51
 */
public class TrafficBarView extends View {

    /*
    * Traffic Color
    * */
    private static final int TRAFFIC_NO_COLOR = Color.GREEN;
    private static final int TRAFFIC_HEAVY_COLOR = Color.RED;
    private static final int TRAFFIC_NORMAL_COLOR = Color.YELLOW;
    /*
    * Total Road Length
     */
    private float totalLength;

    private boolean isVertial;

    /*
    * Traffic section definition
     */
    public class TrafficSection {
        public float offset;
        public float length;
        public int trafficStatus;
    }

    private List<TrafficSection> trafficSections;
    /**
     * 分段颜色
     */
    private static final int[] SECTION_COLORS = {Color.GREEN, Color.YELLOW, Color.RED};
    /**
     * 进度条最大值
     */
    private float maxCount;
    /**
     * 进度条当前值
     */
    private float currentCount;
    /*
    *
    */
    private Paint mPaint;
    private int mWidth, mHeight;

    public TrafficBarView(Context context, AttributeSet attrs,
                          int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    public TrafficBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public TrafficBarView(Context context) {
        super(context);
        initView(context);
    }

    private void initView(Context context) {
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        int round = mHeight / 2;
        System.out.println("max=" + maxCount + "  current=" + currentCount);
//        mPaint.setColor(Color.rgb(71, 76, 80));
        mPaint.setColor(TRAFFIC_NO_COLOR);
        RectF rectBg = new RectF(0, 0, mWidth, mHeight);
//        canvas.drawRoundRect(rectBg, round, round, mPaint);
        canvas.drawRect(rectBg, mPaint);
//        mPaint.setColor(Color.BLACK);
//        RectF rectBlackBg = new RectF(2, 2, mWidth-2, mHeight-2);
//        canvas.drawRoundRect(rectBlackBg, round, round, mPaint);

        drawTrafficSections(canvas);

//        float section = currentCount/maxCount;
//        RectF rectProgressBg = new RectF(3, 3, (mWidth-3)*section, mHeight-3);
//        if(section <= 1.0f/3.0f){
//            if(section != 0.0f){
//                mPaint.setColor(SECTION_COLORS[0]);
//            }else{
//                mPaint.setColor(Color.TRANSPARENT);
//            }
//        }else{
//            int count = (section <= 1.0f/3.0f*2 ) ? 2 : 3;
//            int[] colors = new int[count];
//            System.arraycopy(SECTION_COLORS, 0, colors, 0, count);
//            float[] positions = new float[count];
//            if(count == 2){
//                positions[0] = 0.0f;
//                positions[1] = 1.0f-positions[0];
//            }else{
//                positions[0] = 0.0f;
//                positions[1] = (maxCount/3)/currentCount;
//                positions[2] = 1.0f-positions[0]*2;
//            }
//            positions[positions.length-1] = 1.0f;
//            LinearGradient shader = new LinearGradient(3, 3, (mWidth-3)*section, mHeight-3, colors,null, Shader.TileMode.MIRROR);
//            mPaint.setShader(shader);
//        }
//        canvas.drawRoundRect(rectProgressBg, round, round, mPaint);
    }

    private int dipToPx(int dip) {
        float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));
    }

    public void setTotalLength(float totalLength) {
        this.totalLength = totalLength;
    }

    public void setTrafficSections(List<TrafficSection> sections) {
        this.trafficSections = sections;
    }

    private void drawTrafficSections(Canvas canvas) {

        // fake data
        trafficSections = new ArrayList<>();
        TrafficSection ts = new TrafficSection();
        ts.length = 500;
        ts.offset = 1000;
        ts.trafficStatus = 1;
        trafficSections.add(ts);

        TrafficSection ts1 = new TrafficSection();
        ts1.length = 1000;
        ts1.offset = 6000;
        ts1.trafficStatus = 2;
        trafficSections.add(ts1);

        TrafficSection ts2 = new TrafficSection();
        ts2.length = 1000;
        ts2.offset = 15000;
        ts2.trafficStatus = 2;
        trafficSections.add(ts2);

        totalLength = 20000;
        //isVertial = false;
        // end of fake data

        for (TrafficSection s : trafficSections
                ) {
            drawTrafficSection(s, canvas);
        }


    }

    private void drawTrafficSection(TrafficSection section, Canvas canvas) {
        float x, y, w, h;
        if (isVertial) {
            x = 0;
            y = section.offset / totalLength * mHeight;
            w = mWidth;
            h = section.length / totalLength * mHeight;
            RectF sect = new RectF(x, y, x + w, y + h);
            mPaint.setColor(section.trafficStatus == 1 ? TRAFFIC_NORMAL_COLOR : TRAFFIC_HEAVY_COLOR);
            canvas.drawRect(sect, mPaint);
        } else {
            x = section.offset / totalLength * mWidth;
            y = 0;
            w = section.length / totalLength * mWidth;
            h = mHeight;
            RectF sect = new RectF(x, y, x + w, y + h);
            mPaint.setColor(section.trafficStatus == 1 ? TRAFFIC_NORMAL_COLOR : TRAFFIC_HEAVY_COLOR);
            canvas.drawRect(sect, mPaint);
        }


    }

    /***
     * 设置最大的进度值
     *
     * @param maxCount
     */
    public void setMaxCount(float maxCount) {
        this.maxCount = maxCount;
    }

    /***
     * 设置当前的进度值
     *
     * @param currentCount
     */
    public void setCurrentCount(float currentCount) {
        this.currentCount = currentCount > maxCount ? maxCount : currentCount;
        invalidate();
    }

    public float getMaxCount() {
        return maxCount;
    }

    public float getCurrentCount() {
        return currentCount;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
        if (widthSpecMode == MeasureSpec.EXACTLY || widthSpecMode == MeasureSpec.AT_MOST) {
            mWidth = widthSpecSize;
        } else {
            mWidth = 0;
        }
        if (heightSpecMode == MeasureSpec.AT_MOST || heightSpecMode == MeasureSpec.UNSPECIFIED) {
            mHeight = dipToPx(15);
        } else {
            mHeight = heightSpecSize;
        }
        setMeasuredDimension(mWidth, mHeight);

        isVertial = (mWidth > mHeight) ? false : true;
    }


}
